//------------------------------------------------------------
//  Filename: vga_wrapper.v
//   
//  Author  : wlduan@gmail.com
//  Revise  : 2016-10-11 09:26
//  Description: 
//   
//  Copyright (C) 2014, YRBD, Inc. 					      
//  All Rights Reserved.                                       
//-------------------------------------------------------------
//
`timescale 1ns/1ps
 
module VGA_WRAPPER #(
    parameter integer C_M_AXI_BURST_LEN      = 64,
    parameter integer C_M_AXI_ID_WIDTH       = 1,
    parameter integer C_M_AXI_ADDR_WIDTH     = 32,
    parameter integer C_M_AXI_DATA_WIDTH     = 32,
    parameter integer C_M_AXI_AWUSER_WIDTH   = 0,
    parameter integer C_M_AXI_ARUSER_WIDTH   = 0,
    parameter integer C_M_AXI_WUSER_WIDTH    = 0,
    parameter integer C_M_AXI_RUSER_WIDTH    = 0,
    parameter integer C_M_AXI_BUSER_WIDTH    = 0    
)( 
    //************* Write channel ***********
    input  wire                                 VGA_AXI_ACLK,
    input  wire                                 VGA_AXI_ARESETN,
    output wire [C_M_AXI_ID_WIDTH-1 : 0       ] VGA_AXI_AWID,
    output wire [C_M_AXI_ADDR_WIDTH-1 : 0     ] VGA_AXI_AWADDR,
    output wire [7 : 0                        ] VGA_AXI_AWLEN,
    output wire [2 : 0                        ] VGA_AXI_AWSIZE,
    output wire [1 : 0                        ] VGA_AXI_AWBURST,
    output wire                                 VGA_AXI_AWLOCK,
    output wire [3 : 0                        ] VGA_AXI_AWCACHE,
    output wire [2 : 0                        ] VGA_AXI_AWPROT,
    output wire [3 : 0                        ] VGA_AXI_AWQOS,
    output wire [C_M_AXI_AWUSER_WIDTH-1 : 0   ] VGA_AXI_AWUSER,
    output wire                                 VGA_AXI_AWVALID,
    input  wire                                 VGA_AXI_AWREADY,
    output wire [C_M_AXI_DATA_WIDTH-1 : 0     ] VGA_AXI_WDATA,
    output wire [C_M_AXI_DATA_WIDTH/8-1 : 0   ] VGA_AXI_WSTRB,
    output wire                                 VGA_AXI_WLAST,
    output wire [C_M_AXI_WUSER_WIDTH-1 : 0    ] VGA_AXI_WUSER,
    output wire                                 VGA_AXI_WVALID,
    input  wire                                 VGA_AXI_WREADY,
    input  wire [C_M_AXI_ID_WIDTH-1 : 0       ] VGA_AXI_BID,
    input  wire [1 : 0                        ] VGA_AXI_BRESP,
    input  wire [C_M_AXI_BUSER_WIDTH-1 : 0    ] VGA_AXI_BUSER,
    input  wire                                 VGA_AXI_BVALID,
    output wire                                 VGA_AXI_BREADY,
    //************* Read channel ***********  
    output wire [C_M_AXI_ID_WIDTH-1 : 0       ] VGA_AXI_ARID,
    output wire [C_M_AXI_ADDR_WIDTH-1 : 0     ] VGA_AXI_ARADDR,
    output wire [7 : 0                        ] VGA_AXI_ARLEN,
    output wire [2 : 0                        ] VGA_AXI_ARSIZE,
    output wire [1 : 0                        ] VGA_AXI_ARBURST,
    output wire                                 VGA_AXI_ARLOCK,
    output wire [3 : 0                        ] VGA_AXI_ARCACHE,
    output wire [2 : 0                        ] VGA_AXI_ARPROT,
    output wire [3 : 0                        ] VGA_AXI_ARQOS,
    output wire [C_M_AXI_ARUSER_WIDTH-1 : 0   ] VGA_AXI_ARUSER,
    output wire                                 VGA_AXI_ARVALID,
    input  wire                                 VGA_AXI_ARREADY,
    input  wire [C_M_AXI_ID_WIDTH-1 : 0       ] VGA_AXI_RID,
    input  wire [C_M_AXI_DATA_WIDTH-1 : 0     ] VGA_AXI_RDATA,
    input  wire [1 : 0                        ] VGA_AXI_RRESP,
    input  wire                                 VGA_AXI_RLAST,
    input  wire [C_M_AXI_RUSER_WIDTH-1 : 0    ] VGA_AXI_RUSER,
    input  wire                                 VGA_AXI_RVALID,
    output wire                                 VGA_AXI_RREADY, 
    //--------------------------------------------------------
    input  wire        clk_25mhz,
    input  wire        clk_175mhz,
    input  wire        resetn,
    
    output wire [4:0]  R,  //RGB565 VGA Interface
    output wire [5:0]  G,
    output wire [4:0]  B,
    output wire        vga_de,
    output wire        vga_pclk,
    output wire        vga_h_sync,  
    output wire        vga_v_sync,    

    input  wire        mm_disp_en,   
    input  wire        mm_menu_en,   
    input  wire        mm_bar_hide,   
    input  wire        video0_only,
    input  wire        video1_only,
    input  wire [31:0] video0_base_addr,
    input  wire [31:0] video1_base_addr,
    input  wire [31:0] menu_base_addr,
    input  wire [31:0] pix_cnt       ,
    input  wire [7:0]  menu_alpha    , 
    input  wire [7:0]  disp_graym    , 
    input  wire [7:0]  backlight_pwm_ctnr    ,
    output wire        backlight_pwm, 
    output wire [7:0 ] data_flow_err 
);   
//--------------------------------------------------------
wire        disp_rd_clk;
wire        disp_rd_en ;
wire        disp_empty ;
wire [32:0] disp_dout  ;
wire [32:0] menu_dout  ;
//--------------------------------------------------------
wire [7:0]  vga_R;
wire [7:0]  vga_G;
wire [7:0]  vga_B;
wire        vga_de;
wire        vga_pclk;
wire        vga_h_sync;  
wire        vga_v_sync;  
wire        clk_vga_out; 
//--------------------------------------------------------
wire        clk_vga    = clk_25mhz;
//wire        clk_vga    = clk_vga_out;
wire        clk_7x_vga = clk_175mhz;
//--------------------------------------------------------
VGA_DMA_M00_AXI #( 
    .C_M_AXI_BURST_LEN    ( C_M_AXI_BURST_LEN    ),
    .C_M_AXI_ID_WIDTH     ( C_M_AXI_ID_WIDTH     ),
    .C_M_AXI_ADDR_WIDTH   ( C_M_AXI_ADDR_WIDTH   ),
    .C_M_AXI_DATA_WIDTH   ( C_M_AXI_DATA_WIDTH   ),
    .C_M_AXI_AWUSER_WIDTH ( C_M_AXI_AWUSER_WIDTH ),
    .C_M_AXI_ARUSER_WIDTH ( C_M_AXI_ARUSER_WIDTH ),
    .C_M_AXI_WUSER_WIDTH  ( C_M_AXI_WUSER_WIDTH  ),
    .C_M_AXI_RUSER_WIDTH  ( C_M_AXI_RUSER_WIDTH  ),
    .C_M_AXI_BUSER_WIDTH  ( C_M_AXI_BUSER_WIDTH  )
)VGA_DMA_VGA_AXI_inst0( 
    .M_AXI_ACLK      ( VGA_AXI_ACLK       ) ,
    .M_AXI_ARESETN   ( VGA_AXI_ARESETN    ) ,
    .M_AXI_AWID      ( VGA_AXI_AWID       ) ,
    .M_AXI_AWADDR    ( VGA_AXI_AWADDR     ) ,
    .M_AXI_AWLEN     ( VGA_AXI_AWLEN      ) ,
    .M_AXI_AWSIZE    ( VGA_AXI_AWSIZE     ) ,
    .M_AXI_AWBURST   ( VGA_AXI_AWBURST    ) ,
    .M_AXI_AWLOCK    ( VGA_AXI_AWLOCK     ) ,
    .M_AXI_AWCACHE   ( VGA_AXI_AWCACHE    ) ,
    .M_AXI_AWPROT    ( VGA_AXI_AWPROT     ) ,
    .M_AXI_AWQOS     ( VGA_AXI_AWQOS      ) ,
    .M_AXI_AWUSER    ( VGA_AXI_AWUSER     ) ,
    .M_AXI_AWVALID   ( VGA_AXI_AWVALID    ) ,
    .M_AXI_AWREADY   ( VGA_AXI_AWREADY    ) ,
    .M_AXI_WDATA     ( VGA_AXI_WDATA      ) ,
    .M_AXI_WSTRB     ( VGA_AXI_WSTRB      ) ,
    .M_AXI_WLAST     ( VGA_AXI_WLAST      ) ,
    .M_AXI_WUSER     ( VGA_AXI_WUSER      ) ,
    .M_AXI_WVALID    ( VGA_AXI_WVALID     ) ,
    .M_AXI_WREADY    ( VGA_AXI_WREADY     ) ,
    .M_AXI_BID       ( VGA_AXI_BID        ) ,
    .M_AXI_BRESP     ( VGA_AXI_BRESP      ) ,
    .M_AXI_BUSER     ( VGA_AXI_BUSER      ) ,
    .M_AXI_BVALID    ( VGA_AXI_BVALID     ) ,
    .M_AXI_BREADY    ( VGA_AXI_BREADY     ) ,
    .M_AXI_ARID      ( VGA_AXI_ARID       ) ,
    .M_AXI_ARADDR    ( VGA_AXI_ARADDR     ) ,
    .M_AXI_ARLEN     ( VGA_AXI_ARLEN      ) ,
    .M_AXI_ARSIZE    ( VGA_AXI_ARSIZE     ) ,
    .M_AXI_ARBURST   ( VGA_AXI_ARBURST    ) ,
    .M_AXI_ARLOCK    ( VGA_AXI_ARLOCK     ) ,
    .M_AXI_ARCACHE   ( VGA_AXI_ARCACHE    ) ,
    .M_AXI_ARPROT    ( VGA_AXI_ARPROT     ) ,
    .M_AXI_ARQOS     ( VGA_AXI_ARQOS      ) ,
    .M_AXI_ARUSER    ( VGA_AXI_ARUSER     ) ,
    .M_AXI_ARVALID   ( VGA_AXI_ARVALID    ) ,
    .M_AXI_ARREADY   ( VGA_AXI_ARREADY    ) ,
    .M_AXI_RID       ( VGA_AXI_RID        ) ,
    .M_AXI_RDATA     ( VGA_AXI_RDATA      ) ,
    .M_AXI_RRESP     ( VGA_AXI_RRESP      ) ,
    .M_AXI_RLAST     ( VGA_AXI_RLAST      ) ,
    .M_AXI_RUSER     ( VGA_AXI_RUSER      ) ,
    .M_AXI_RVALID    ( VGA_AXI_RVALID     ) ,
    .M_AXI_RREADY    ( VGA_AXI_RREADY     ) ,
    .disp_rd_clk     ( disp_rd_clk        ) ,
    .disp_rd_en      ( disp_rd_en         ) ,
    .disp_empty      ( disp_empty         ) ,
    .disp_dout       ( disp_dout          ) ,
    .menu_dout       ( menu_dout          ) ,
    .menu_base_addr  ( menu_base_addr     ) ,
    .video0_only     ( video0_only        ) ,
    .video1_only     ( video1_only        ) ,
    .video0_base_addr( video0_base_addr   ) ,
    .video1_base_addr( video1_base_addr   ) ,
    .disp_flow_err   ( data_flow_err[1:0] ) ,
    .menu_flow_err   ( data_flow_err[3:2] ) ,
    .pix_cnt         ( pix_cnt            ) ,
    .vga_v_sync      ( vga_v_sync         ) ,
    .mm_disp_en      ( mm_disp_en         ) ,
    .mm_menu_en      ( mm_menu_en         ) 
);  
//--------------------------------------------------------
VGA_DRIVER VGA_DRIVER_inst0( 
     .clk_vga        ( clk_vga            ) ,
     .rst            ( ~resetn            ) ,
     
     .vga_R          ( vga_R              ) ,
     .vga_G          ( vga_G              ) ,
     .vga_B          ( vga_B              ) ,
     .vga_pclk       ( vga_pclk           ) ,
     .vga_h_sync     ( vga_h_sync         ) ,
     .vga_v_sync     ( vga_v_sync         ) ,
     .vga_de         ( vga_de             ) ,
    
     .mm_bar_hide    ( mm_bar_hide        ) ,
     .mm_menu_en     ( mm_menu_en         ) ,
     .mm_disp_en     ( mm_disp_en         ) ,
     .menu_alpha     ( menu_alpha         ) ,
     .disp_rd_clk    ( disp_rd_clk        ) ,
     .disp_rd_en     ( disp_rd_en         ) ,
     .disp_empty     ( disp_empty         ) ,
     .disp_dout      ( disp_dout          ) ,
     .disp_graym     ( disp_graym         ) ,
     .menu_dout      ( menu_dout          ) 
); 
//--------------------------------------------------------
VGA_BACKLIGHT  VGA_BACKLIGHT_inst0(
     .clk_vga                ( clk_vga                    ) ,
     .rst                    ( ~resetn                    ) ,

     .backlight_pwm_ctnr     ( backlight_pwm_ctnr         ) ,
     .backlight_pwm          ( backlight_pwm              ) 
);
//--------------------------------------------------------
assign R = vga_R[7:3];
assign G = vga_G[7:2];
assign B = vga_B[7:3];

endmodule
